package com.test.www.service;

import com.test.www.entity.Student;
import com.test.www.util.FileUtil;
import com.test.www.util.IArrayList;
import sun.tools.jar.Main;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * <p>学生信息总览页面</p>
 * @author
 * 2020/12/17 21:41
 */
public class AllStudent {
    public AllStudent(String msg) throws IOException {
        JFrame jFrame = new JFrame("学生成绩管理系统（学生信息总览页面）");

        JPopupMenu jPopupMenu = new JPopupMenu();
        JPanel jPanel = new JPanel();
        JLabel temp1 = new JLabel("---------------------------------------------");
        JButton jButton1 = new JButton("这里是学生信息总览页面");
        JButton jButton2 = new JButton("返回");
        JButton update = new JButton("修改");
        JTextField rowText = new JTextField();
        rowText.setColumns(3);
        rowText.setSize(100,36);
        JButton delete = new JButton("删除");
        JLabel temp2 = new JLabel("---------------------------------------------");
        JLabel msg1 = new JLabel("在此输入你想要删除的学生信息的行数：");
        JLabel msg2 = new JLabel("修改提醒：对表格中的学生信息修改后，点击修改即可（注意：总分由系统计算，无法修改）");

        jFrame.setSize(850, 550);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
        if (msg!=null){
            jPopupMenu.removeAll();
            jPopupMenu.add(msg);
            jPopupMenu.show(jFrame,360,250);
        }

        // 返回
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Home();
                jFrame.dispose();
            }
        });

        // 表头（列名）
        Object[] columnNames = {"行数","学号", "姓名", "数学", "语文", "英语", "物理", "计算机","总分"};

        // 获取全部学生信息
        IArrayList<Student> list = FileUtil.getInstance().getStudent();
        // 表格所有行数据
        Object[][] rowData = new Object[list.size()][9];
        int i = 0;
        for (Student student : list) {
            rowData[i][0] = i+1;
            rowData[i][1] = student.getId();
            rowData[i][2] = student.getUsername();
            rowData[i][3] = student.getMath();
            rowData[i][4] = student.getChinese();
            rowData[i][5] = student.getEnglish();
            rowData[i][6] = student.getPhysics();
            rowData[i][7] = student.getComputer();
            rowData[i][8] = student.getComputer()+student.getPhysics()+student.getEnglish()+student.getMath()+student.getChinese();
            i++;
        }

        // 创建 表格模型，指定 所有行数据 和 表头
        TableModel tableModel = new DefaultTableModel(rowData, columnNames);
        // 使用 表格模型 创建 表格
        JTable jTable = new JTable(tableModel){
            // 行数和总成绩不允许被编辑
            public boolean isCellEditable(int row, int column) {
                if (column==0 || column==8){
                    return false;
                }
                return true;
            }
        };



        // 使用 表格模型 创建 行排序器（TableRowSorter 实现了 RowSorter）
        RowSorter<TableModel> rowSorter = new TableRowSorter<TableModel>(tableModel);
        // 给 表格 设置 行排序器
        jTable.setRowSorter(rowSorter);
        // 当其他控件获取焦点后，表格会停止编辑
        jTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

        // 修改
        new ButtonService(new JTextField(),update,jFrame,1,rowText,jTable);
        // 删除
        new ButtonService(new JTextField(),delete,jFrame,-1,rowText,jTable);

        //字体大小设置
        Font font = new Font("宋体",Font.BOLD,18);
        jButton1.setFont(font);
        jButton2.setFont(font);
        update.setFont(font);
        delete.setFont(font);
        msg1.setFont(font);
        //表格内容字体大小
        Font font1 = new Font("宋体",Font.PLAIN,16);
        jTable.setFont(font1);
        //表格高度
        jTable.setRowHeight(30);

        jPanel.add(temp1);
        jPanel.add(jButton1);
        jPanel.add(jButton2);
        jPanel.add(temp2);

        jPanel.add(jTable.getTableHeader(),BorderLayout.NORTH);
        jPanel.add(jTable,BorderLayout.CENTER);
        jPanel.add(msg1);
        jPanel.add(rowText,BorderLayout.EAST);
        jPanel.add(delete,BorderLayout.EAST);
        jPanel.add(update,BorderLayout.EAST);
        jPanel.add(msg2);

        jFrame.add(jPanel);
        jFrame.setVisible(true);
    }
}

/**
 * <p>修改、删除学生信息</p>
 *
 * @author
 * 2020/12/18 13:59
 */
class ButtonService extends DefaultCellEditor {
    JPopupMenu jPopupMenu = new JPopupMenu();

    // 修改或删除操作
    public ButtonService(JTextField checkBox,JButton button,JFrame jFrame,int flag,JTextField rowText,JTable jTable) {
        super(checkBox);

        button.addActionListener(new ActionListener() {
            // 执行操作
            public void actionPerformed(ActionEvent e) {
                jPopupMenu.removeAll();
                try {
                    if (flag==1){// 执行更新操作
                        FileUtil.getInstance().updateStudent(jTable);
                        new AllStudent("修改成功!");
                        jFrame.dispose();
                    }
                    if (flag==-1){// 执行删除操作
                        int row = Integer.valueOf(rowText.getText())-1;
                        FileUtil.getInstance().deleteStudent(row,jTable);
                        new AllStudent("删除成功!");
                        jFrame.dispose();
                    }
                } catch (Exception ex) {
                    try {
                        new AllStudent("操作失败!");
                        jFrame.dispose();
                    } catch (IOException exc) {
                        exc.printStackTrace();
                    }
                }
                jFrame.dispose();
            }
        });
    }

}
